package com.cx.base.day22_regular_expression;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reptile {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        test();
//        String str = null;
//        List<String> list = new ArrayList<String>();
//        String mail = "\\w+@\\w+(\\.\\w)+";
//        try {
//            URL url = new URL("https://mail.163.com/js6/main.jsp?sid=sCOvsXotluFgskVzErttnxUxEdzQVGVq&df=mail163_letter#module=read.ReadModule%7C%7B%22area%22%3A%22normal%22%2C%22isThread%22%3Afalse%2C%22viewType%22%3A%22%22%2C%22id%22%3A%22325%3AxtbBRRj63lPAFHSQBAAAse%22%2C%22fid%22%3A18%7D");
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
//            while ((str = bufferedReader.readLine())!=null){
//                Pattern p = Pattern.compile(mail);
//                Matcher m = p.matcher(str);
//                while (m.find()){
//                    list.add(m.group());
//                }
//            }
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        for (String s:list){
//            System.out.println(s);
//        }

        match();
        matchFileKeyWords();
        judgeEmailPattern();
        zhikouchi();
        matchAndGet();
    }

    public static void test() {
        String str = "zhangttttttttttfalghalg";
        System.out.println(str.replaceAll("(.)\\1+", "$1"));
        String[] strs = str.split("(.)\\1+");
        for (int i = 0; i < strs.length; i++) {

            System.out.println(strs[i].toString());
        }
        System.out.println(".");
    }

    public static void match() {
        String tel = "15225723153";
        String regex = "1[356789][0-9]{9}";
        System.out.println(tel.matches(regex));
    }

    public static void matchFileKeyWords() throws IOException, ClassNotFoundException {
        String filePath = Class.forName("com.cx.base.day22_regular_expression.Reptile").getClassLoader().getResource("reptile.txt").getFile();
        File my = new File(filePath);
        String filePath2 = Class.forName("com.cx.base.day22_regular_expression.Reptile").getClassLoader().getResource("reptile_write.txt").getFile();
        System.out.println(filePath + "\n" + filePath2);
        File my2 = new File(filePath2);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(my), StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(my2), StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        String lineTxt;
        int i = 0;
        while ((lineTxt = br.readLine()) != null) {
            if (lineTxt.matches("[J[F]][-]\\d{2}")) {
                System.out.println(lineTxt);
                bw.write(lineTxt);
                bw.newLine();
            }
        }
        br.close();
    }

    public static void judgeEmailPattern() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个邮箱：");
        String email = sc.next();
        String reg = "[A-Za-z0-9_]{1,}[@][0-9a-z]{2,3}[.][a-z]{2,3}";
        if (email.matches(reg)) {
            System.out.println("输入邮箱合法！");
        } else {
            System.out.println("输入邮箱不合法！");
        }
    }

    //    练习 治口吃
    public static void zhikouchi() {
//        提炼出 我要学编程
        String str = "我我我......我我要要要....要。。。学编.....程";
//        先去除... 。。。
        str = str.replaceAll("[.。]+", "");
//        再去重复汉字
        str = str.replaceAll("(.)\\1+", "$1");
        System.out.println(str);

    }

    public static void matchAndGet() {
        String str = "我我我......我我要要要....要。。。学编.....程";
        Pattern pattern = Pattern.compile("(.)\\1+");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
            System.out.println(matcher.start() + " & " + matcher.end());
        }
    }
}
